<?php

/**
 * This is the model class for table "user_favorite".
 *
 * The followings are the available columns in table 'user_favorite':
 * @property integer $user_id
 * @property integer $user_favorited
 */
class mUserFavorite extends CActiveRecord {

  /**
   * Returns the static model of the specified AR class.
   * @param string $className active record class name.
   * @return mUserFavorite the static model class
   */
  public static function model($className = __CLASS__) {
    return parent::model($className);
  }

  /**
   * @return string the associated database table name
   */
  public function tableName() {
    return 'user_favorite';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('user_id, user_favorited', 'required'),
        array('user_id, user_favorited', 'numerical', 'integerOnly' => true),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('user_id, user_favorited', 'safe', 'on' => 'search'),
    );
  }

  /**
   * @return array relational rules.
   */
  public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels() {
    return array(
        'user_id' => 'User',
        'user_favorited' => 'User Favorited',
    );
  }

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search() {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria = new CDbCriteria;

    $criteria->compare('user_id', $this->user_id);
    $criteria->compare('user_favorited', $this->user_favorited);

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
  }

  public function insertFavorite($user_id, $user_favorited) {
    $command = db()->createCommand();
    $command->insert('user_favorite', array(
        'user_id' => $user_id,
        'user_favorited' => $user_favorited,
    ));
  }

  public static function getUserFavorite($user_id, $user_favorited){
    $user = Yii::app()->db->createCommand()
                  ->select('*')
                  ->from('user_favorite uf')
                  ->where('user_id=:user_id and user_favorited=:user_fb',
                              array(':user_id'=>$user_id,':user_fb'=>$user_favorited))
                  ->queryRow();
    return $user;
  }
  
  public function getUserFavorited($user_favorited){
      $user = Yii::app()->db->createCommand()
                  ->select('uv.*, up.user_id as user_idup, up.link_img, p.gender, p.birthdate')
                  ->from('user_favorite uv')
                  ->leftjoin('profiles p', 'p.user_id = uv.user_favorited')
                  ->leftjoin('user_photos up', 'up.approve = 1 and up.make_primary = 1 and up.user_id = uv.user_favorited')
                  ->where('uv.user_id=:user_id', array(':user_id'=>$user_favorited))
                  ->limit(3)
                  ->queryAll();
    
      if(count($user)>0)
        return $user;
      else
        return false;
  }
  
  public function getUserFavoriteWithPagination($user_id, $offset, $limit){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*, uf.date_favorite as date_view, u.username, up.link_img, ct.name as country_name, uf.user_favorited')
                  ->from('user_favorite uf')
                  ->leftjoin('profiles p', 'p.user_id=uf.user_favorited')
                  ->leftjoin('users u', 'u.user_id=uf.user_favorited')
                  ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uf.user_favorited')
                  ->leftjoin('countries ct', 'ct.code = p.country')
                  ->where('uf.user_id=:user_id',array(':user_id'=>$user_id))
                  ->limit($limit,$offset)
                  ->queryAll();
      return $user;
    }
    
    public function getUserFavoritedWithPagination($user_favorited, $offset, $limit){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*, uf.date_favorite as date_view, u.username, up.link_img, ct.name as country_name, uf1.user_favorited')
                  ->from('user_favorite uf')
                  ->leftjoin('profiles p', 'p.user_id=uf.user_id')
                  ->leftjoin('users u', 'u.user_id=uf.user_id')
                  ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uf.user_id')
                  ->leftjoin('user_favorite uf1', 'uf1.user_id=:user_id and uf1.user_favorited=uf.user_id',array(':user_id'=>$user_favorited))
                  ->leftjoin('countries ct', 'ct.code = p.country')
                  ->where('uf.user_favorited=:user_favorited and uf.mutual = 0',array(':user_favorited'=>$user_favorited))
                  ->limit($limit,$offset)
                  ->queryAll();
      return $user;
    }

    public function getUserFavoritedMutualWithPagination($user_favorited, $offset, $limit){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*, uf.date_favorite as date_view, u.username, up.link_img, ct.name as country_name, uf1.user_favorited')
                  ->from('user_favorite uf')
                  ->leftjoin('profiles p', 'p.user_id=uf.user_id')
                  ->leftjoin('users u', 'u.user_id=uf.user_id')
                  ->leftjoin('user_photos up', 'up.approve=1 and up.make_primary=1 and up.user_id=uf.user_id')
                  ->leftjoin('user_favorite uf1', 'uf1.user_id=:user_id and uf1.user_favorited=uf.user_id',array(':user_id'=>$user_favorited))
                  ->leftjoin('countries ct', 'ct.code = p.country')
                  ->where('uf.user_favorited=:user_favorited and uf.mutual = 1',array(':user_favorited'=>$user_favorited))
                  ->limit($limit,$offset)
                  ->queryAll();
      return $user;
    }
}